package com.steven.android40_weathermvp.view.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.steven.android40_weathermvp.R;
import com.steven.android40_weathermvp.adapter.WeatherAdapter;
import com.steven.android40_weathermvp.bean.WeatherBean;
import com.steven.android40_weathermvp.presenter.IWeatherPresenter;
import com.steven.android40_weathermvp.presenter.WeatherPresenterImpl;
import com.steven.android40_weathermvp.view.interfaces.IWeatherView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements IWeatherView {
    private Context mContext = this;
    private IWeatherPresenter mWeatherPresenter;
    private TextView textView_city;
    private ProgressBar progressBar_main;
    private ListView listView_main;
    private List<WeatherBean.DataEntity.ForecastEntity> mTotalList = new ArrayList<>();
    private WeatherAdapter mAdapter = null;
    private String mCity = "大连";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

        loadData();
    }

    private void initView() {
        textView_city = (TextView) findViewById(R.id.textView_city);
        textView_city.setText(mCity);
        progressBar_main = (ProgressBar) findViewById(R.id.progressBar_main);

        listView_main = (ListView) findViewById(R.id.listView_main);
        mAdapter = new WeatherAdapter(mContext, mTotalList);
        listView_main.setAdapter(mAdapter);
    }

    private void loadData() {
        mWeatherPresenter = new WeatherPresenterImpl(this);
        mWeatherPresenter.loadWeatherData(mCity);
    }

    @Override
    public void showProgress() {
        progressBar_main.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideProgress() {
        progressBar_main.setVisibility(View.GONE);
    }

    @Override
    public void showErrorInfo(String msg) {
        Snackbar.make(listView_main, msg, Snackbar.LENGTH_SHORT).show();
    }

    @Override
    public void loadWeatherData(WeatherBean weatherBean) {
        List<WeatherBean.DataEntity.ForecastEntity> list = weatherBean.getData().getForecast();
        mAdapter.reloadListView(list, true);

    }
}
